package com.a0soft.gphone.aTruffleHog.wnd;
/*
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 3, or (at your option) any
 * later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * Copyright(C) 2011 
 *   Sam Lu <ysl.sam.lu (at) gmail.com>
 *
 */

import com.a0soft.gphone.aTruffleHog.gio.ui.BaseActivity;
import com.a0soft.gphone.aTruffleHog.srvc.HttpConnSrvc;
import com.a0soft.gphone.aTruffleHog.util.aDebug;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.app.Fragment;

public class BaseFragment extends Fragment {
    private BroadcastReceiver  m_br;
    
    private static final String LOG_TAG = BaseFragment.class.getSimpleName();
    
    //////////////////////////////////////
    /*@Override public void 
    onCreate(Bundle bdSavedInstanceState)
    {
        OnCreate(bdSavedInstanceState, false);
    }
    
    public void 
    OnCreate(Bundle bdSavedInstanceState, boolean bReceiveDownloadNotification)
    {
        if (aDebug.ENABLED)
            aDebug.d(LOG_TAG, "onCreate(): bReceiveDownloadNotification=" + bReceiveDownloadNotification);
        super.onCreate(bdSavedInstanceState);
        
        if (bReceiveDownloadNotification) {
            _InitBroadcastReceiver();
            
            IntentFilter itf = new IntentFilter(HttpConnSrvc.ACTION_DOWNLOAD_STATUS);
            getActivity().registerReceiver(m_br, itf);
        }
    }*/

    @Override public void 
    onActivityCreated(Bundle bdSavedInstanceState)
    {
        OnActivityCreated(bdSavedInstanceState, false);
    }

    public void 
    OnActivityCreated(Bundle bdSavedInstanceState, boolean bReceiveDownloadNotification)
    {
        if (aDebug.ENABLED)
            aDebug.d(LOG_TAG, "onCreate(): bReceiveDownloadNotification=" + bReceiveDownloadNotification);
        super.onCreate(bdSavedInstanceState);
        
        if (bReceiveDownloadNotification) {
            _InitBroadcastReceiver();
            
            IntentFilter itf = new IntentFilter(HttpConnSrvc.ACTION_DOWNLOAD_STATUS);
            getActivity().registerReceiver(m_br, itf);
        }
    }
    
    @Override public void 
    onDestroy()
    {
        if (aDebug.ENABLED)
            aDebug.d(LOG_TAG, "onDestroy()");
        if (m_br != null) {
            getActivity().unregisterReceiver(m_br);
            m_br = null;
        }
        
        super.onDestroy();
    }
    
    public final BaseActivity
    GetBaseActivity()
    {
        return (BaseActivity)getActivity();
    }
    
    private void
    _InitBroadcastReceiver()
    {
        m_br = new BroadcastReceiver() {            
            @Override public void 
            onReceive(Context context, Intent it)
            {
                if (it == null) return;
                final String strAction = it.getAction();
                if (strAction == null) return;
                
                if (aDebug.ENABLED)
                    aDebug.d(LOG_TAG, "strAction=" + strAction);
                
                if (strAction.equals(HttpConnSrvc.ACTION_DOWNLOAD_STATUS)) {
                    int nStatus = it.getIntExtra(HttpConnSrvc.EXTRA_DOWNLOAD_STATUS, 
                                                 HttpConnSrvc.DOWNLOAD_STATUS_UNKNOWN);
                    int nCmd = it.getIntExtra(HttpConnSrvc.EXTRA_CMD, 
                                              HttpConnSrvc.CMD_UNKNOWN);

                    String strResultOrError = it.getStringExtra(HttpConnSrvc.EXTRA_DOWNLOAD_CONTENT);

                    if (aDebug.ENABLED) {
                        aDebug.d(LOG_TAG, "  nCmd=" + nCmd);
                        aDebug.d(LOG_TAG, "  nStatus=" + nStatus);
                    }
                    
                    switch (nStatus) {
                    case HttpConnSrvc.DOWNLOAD_STATUS_START:
                        OnDownloadStart(nCmd);
                        break;
                    case HttpConnSrvc.DOWNLOAD_STATUS_FINISHED_OKAY:
                        OnDownloadFinishedOkay(nCmd, strResultOrError);
                        break;
                    case HttpConnSrvc.DOWNLOAD_STATUS_FINISHED_ERROR:
                        OnDownloadFinishedError(nCmd, strResultOrError);
                        break;   
                    case HttpConnSrvc.DOWNLOAD_STATUS_STOP:
                        OnDownloadStop(nCmd);
                        break;
                    }
                }
            }
        };
    }
    
    protected void OnDownloadStart(int nCmd) {}
    protected void OnDownloadFinishedOkay(int nCmd, String strResult) {}
    protected void OnDownloadFinishedError(int nCmd, String strError) {}
    protected void OnDownloadStop(int nCmd) {}
    
}
